package net.chathu.airgw.packets.gw.clientpackets 
{
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	import net.chathu.airgw.Config;
	import net.chathu.airgw.Debug;
	import net.chathu.airgw.model.handler.IRCPacketProcessor;
	import net.chathu.airgw.packets.gw.serverpackets.EchoPacket;
	/**
	 * ...
	 * @author ze
	 */
	public class CMDGWHelp extends IRCReceivablePacket 
	{
		
		public function CMDGWHelp() 
		{
			
		}
		
		override public function execute():void {
			var commandsDict:Dictionary = IRCPacketProcessor.availableCommands;
			
			var cmd:String;
			var clazz:Class;
			var desc:String;
			if (data.length > 1) { 
				cmd = data[1];
				clazz = commandsDict[cmd.toUpperCase()] as Class;
				if (clazz) {
					if (typeof clazz["getUsageDescription"] == "function") {
						desc = clazz["getUsageDescription"]();
					} else {
						desc = IRCReceivablePacket.getUsageDescription();
					}
					sendToClient(new EchoPacket(Config.GW_ECHO_NICK, Config.IRC_CHAR_COLOR + "7" + Config.IRC_CHAR_BOLD  + "/" + cmd.toLowerCase() + Config.IRC_CHAR_COLOR + Config.IRC_CHAR_BOLD + " parancs leírása:"));
					var arr:Array = desc.split("\n");
					for(var lineIdx:String in arr) {
						sendToClient(new EchoPacket(Config.GW_ECHO_NICK, arr[lineIdx]));
					}
					sendToClient(new EchoPacket(Config.GW_ECHO_NICK,"-- vége --"));
				} else {
					sendToClient(new EchoPacket(Config.GW_ECHO_NICK,"A megadott parancs (" + cmd + ") nem létezik."));
				}
			} else {
				
				var commandArr:Array = new Array();
				
				for (cmd in commandsDict) {
					commandArr[commandArr.length] = cmd; //faster than array.push();
				}
				commandArr = commandArr.sort(); //abc sorrendben a parancsok:
				
				sendToClient(new EchoPacket(Config.GW_ECHO_NICK, "A használható parancsok listája:"));
				for (var i:int = 0; i < commandArr.length; i++) {
					cmd = commandArr[i];
					
					clazz = commandsDict[cmd] as Class;

					
					if (typeof clazz["getShortUsageDescription"] == "function") {
						desc = clazz["getShortUsageDescription"]();
					} else {
						//desc = IRCReceivablePacket.getUsageDescription();
						desc = IRCReceivablePacket.getShortUsageDescription();
					}
					
					sendToClient(new EchoPacket(Config.GW_ECHO_NICK,Config.IRC_CHAR_COLOR + "7"+ Config.IRC_CHAR_BOLD  + "/" + cmd.toLowerCase() + Config.IRC_CHAR_COLOR + Config.IRC_CHAR_BOLD+ ": " + desc));
				}
				sendToClient(new EchoPacket(Config.GW_ECHO_NICK, "Az egyes parancsokról részletes leírást kaphatsz a /gwhelp COMMAND (pl.: /gwhelp ison) beírásával!"));
			}
		}
		
		public static function getShortUsageDescription():String {
			return "Parancs súgó";
		}
		
		public static function getUsageDescription():String {
			return "Példa:\n" +
				"/gwhelp\n\n" +
				"Eredmény:\n" +
				"Sikeresen megjelenítetted az elérhető parancsok listáját.";
		}
	}

}